package dd.control;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import xml.Message;

import dd.model.Edge;
import dd.model.Event;
import dd.model.Model;
import dd.view.DrawingCanvas;
import dd.view.addEdgeFrame;

public class addEdgemouseController extends MouseAdapter {
	/**
	 * call this controller when a user wants to add an edge
	 * check if this edge is valid(I haven't done that)
	 *  if this edge is valid, send it to the server, and paint it on the screen
	 * if not, let the user repaint(haven't done)
	 */
	
	Point mousePressed;	
	addEdgeFrame aef;
	DrawingCanvas dc;
	Point revised1;
	Point revised2;
	Edge  edge;
	int l;
	int r;
	int h;
	int leftchoice;
	int rightchoice;
	int height;

	public addEdgemouseController(addEdgeFrame a){
		this.aef = a;
		this.dc = aef.getDrawingCanvas();
	}
	@Override
	public void mousePressed(MouseEvent arg) {
		mousePressed = arg.getPoint();
	Model model = Model.getInstance();
	Event e = model.getEvent();
	//if this is your turn
	if(e.getIsYourturn()&&(e.getleftEdges()!=0)){
	for(int i = 0;i<dc.getarrsize();i++){
	if(mousePressed.getX()<dc.getarr(i)){
	l = dc.getarr(i-1);
	leftchoice = i-1;
	r = dc.getarr(i);
	rightchoice = i;
	h = mousePressed.y;
	height = h-dc.getlinetop();
	i = dc.getarrsize();
	}
}
	if(e.getType().equals("roundRobin"))
	e.setIsYourTurn(false);
	dc.drawedge(l,r,h);
	//future should be add at add choiceResponse
	e.leftEdgesDecrement();
//	aef.repaint();

	String leftt = String.valueOf(leftchoice);
	String rightt = String.valueOf(rightchoice);
	String heightt = String.valueOf(height);

	String xmlString = Message.requestHeader() + "<addEdgeRequest id='"+e.getEventID() +"'"+
			" left='"+leftt+ "' right='" + rightt + "' height='"+heightt+"'/>"+
			"</request>";
	Message m = new Message (xmlString);
	model.getServerAccess().sendRequest(m);
	}
	}

}
